﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using RedBit.UserVoice.Services;
using RedBit.UserVoice.Model;
using RedBit.UserVoice.Helpers;

namespace SampleApplication
{
    public partial class AddSuggestionPage : PhoneApplicationPage
    {
        public AddSuggestionPage()
        {
            InitializeComponent();
        }

        private void cbCancel_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

        private void cbSave_Click(object sender, EventArgs e)
        {
            var suggestion = new UVSuggestion
            {
                Title = tbTitle.Text,
                Text = tbDescription.Text,
                VoteCount = 2,
                ForumId = UVUserHelper.CurrentUser.Forums.First().Id
            };

            UVSuggestionService.CreateSuggestion(suggestion, (response) => HandleResponse(response));

        }

        private void HandleResponse(UVResponse<UVSuggestion> response)
        {
            //TODO: Handle Errors
            if (response.Error == null)
            {
                Deployment.Current.Dispatcher.BeginInvoke(() => NavigationService.GoBack());
            }
            else
            {
                Deployment.Current.Dispatcher.BeginInvoke(() => MessageBox.Show(response.Error.Message));
            }
        }
    }
}